﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace Nero_demo
{
    public partial class mainFram : Form
    {
        public mainFram()
        {
            InitializeComponent();

            LogHelper.Init();
        }

        private void mainFram_Load(object sender, EventArgs e)
        {
            processHelper = new ProcessHelper();
            processHelper.Name = "梦幻西游：时空";

            taskManager = new Westward.TaskManager("梦幻西游：时空");

            LogHelper.logEvent += HandleLogEvent;
            InitTestMenu();
            InitTeamMenu();
        }

        private void mainFram_FormClosed(object sender, FormClosedEventArgs e)
        {
            taskManager.OnExit();
        }

        private void InitTestMenu()
        {
            testMenu = new UIContextMenuStrip();
            ToolStripMenuItem findImageItem = new ToolStripMenuItem("查找图像");
            findImageItem.Click += findImageItem_Click;
            testMenu.Items.Add(findImageItem);
            ToolStripMenuItem detectDigitItem = new ToolStripMenuItem("识别数字");
            detectDigitItem.Click += detectDigitItem_Click;
            testMenu.Items.Add(detectDigitItem);
        }

        private void InitTeamMenu()
        {
            teamMenu = new UIContextMenuStrip();
            ToolStripMenuItem ghostItem = new ToolStripMenuItem("捉鬼队");
            ghostItem.Click += ghostItem_click;
            teamMenu.Items.Add(ghostItem);
            ToolStripMenuItem instanceItem = new ToolStripMenuItem("副本队");
            instanceItem.Click += instanceItem_click;
            teamMenu.Items.Add(instanceItem);
        }

        private void instanceItem_click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.SearchInstanceTeam));
        }

        private void ghostItem_click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.SearchGhostTeam));
        }

        private void HandleLogEvent(string logStr)
        {
            if (this.txtLog.InvokeRequired)
            {
                // 如果是跨线程调用，使用Invoke进行调用
                this.txtLog.Invoke(new Action(() =>
                {
                    this.txtLog.AppendText(logStr);
                }));
            }
            else
            {
                // 如果已经是UI线程，直接操作
                this.txtLog.AppendText(logStr);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtLog.Clear();
        }

        private ProcessHelper processHelper;
        private Westward.TaskManager taskManager;

        private void btnStart_Click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.Start));
        }

        private void StartBackgroundTask(ThreadStart ts)
        {
            Thread myThread = new Thread(ts);//实例化Thread线程类对象
            myThread.IsBackground = true;
            myThread.Start();
        }

        private void btnShot_Click(object sender, EventArgs e)
        {
            processHelper.SnapshotCurrent();
        }

        private void TestFindImage()
        {
            Point pos = new Point();
            string sourceFile = null;
            string templateFile = null;

            OpenFileDialog openFileDialog = new OpenFileDialog();

            // 设置对话框的属性
            openFileDialog.InitialDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "snapshot\\"; // 设置初始目录
            openFileDialog.Filter = "图像文件(*.png)|*.png"; // 设置文件过滤器
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;

            openFileDialog.Title = "原图";
            if (openFileDialog.ShowDialog() == DialogResult.OK) // 显示对话框
            {
                // 获取选中的文件路径
                sourceFile = openFileDialog.FileName;
            }
            openFileDialog.Title = "待查找图";
            if (openFileDialog.ShowDialog() == DialogResult.OK) // 显示对话框
            {
                // 获取选中的文件路径
                templateFile = openFileDialog.FileName;
            }
            if (sourceFile != null && templateFile != null)
            {
                ImageSearcher.FindImage(sourceFile, templateFile, out pos, 0.75, true);
            }
        }

        private void findImageItem_Click(object sender, EventArgs e)
        {
            TestFindImage();
        }

        private void detectDigitItem_Click(object sender, EventArgs e)
        {
            // UIMessageBox.Show("没做");
            taskManager.InitProcList();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            // testMenu.Show(btnTest.Location);
        }

        private void btnTest_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                testMenu.Show(btnTest, e.Location);
            }
        }

        private void btnFindTeam_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                teamMenu.Show(btnFindTeam, e.Location);
            }
        }


        private void btnTreasure_Click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.TreasureMap));
        }

        private void btnSecret_Click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.SecretTask));
        }

        private void btnFindTeam_Click(object sender, EventArgs e)
        {
            // StartBackgroundTask(new ThreadStart(processHelper.SearchGhostTeam));
        }

        private void btnScratchCard_Click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.ScratchCard));
        }

        private void btnExpress_Click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.ExpressTask));
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.ReturnChangAn));
        }

        private void btnLike_Click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.LikeTask));
        }

        private void btnInstance_Click(object sender, EventArgs e)
        {
            StartBackgroundTask(new ThreadStart(processHelper.SearchInstanceTeam));
        }

        private void btnArena_Click(object sender, EventArgs e)
        {
            // StartBackgroundTask(new ThreadStart(processHelper.ArenaTask));
            string result = string.Empty;
            taskManager.StartArenaTask(out result);
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            TaskConfigForm tcForm = new TaskConfigForm(processHelper);
            tcForm.ShowDialog();
        }

        private UIContextMenuStrip testMenu;
        private UIContextMenuStrip teamMenu;
    }
}
